<!doctype html>
<html>
<head>
	<meta charset="utf-8"><meta name="format-detection" content="telephone=no">
	<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
	<title>我的</title>
	<link rel="stylesheet" type="text/css" href="../../lib/css/skin.css"/>
	<link rel="stylesheet" type="text/css" href="../../lib/css/mobiscroll.css"/>
</head>
<body>
<div id="root">
	<div class="app_warp">
		<div class="app_head">
			<span class="left back"></span>
			<span class="name"><img src="../../lib/img/loading.gif" class="minimize" alt=""></span>
		</div>
		<div class="nocontent animated fadeInUp">
			<img src="../../lib/img/loading.gif" class="minimize" alt="">
			<div class="text minimize">
				准备数据
			</div>
		</div>
		<div id="usersel" class="app_main animated fadeInUp" style="display: none;padding: 0"></div>
	</div>
</div>
<script id="usersel_template" type="text/html">
	<ul class="app_formlist">
		<% if(data.regType==='student'){ var student = data.student%>
		<li class="app_formlist_avatar">
			<span class="name">
				头像
			</span>
			<span class="val">
				<img id="student_avatar" src="<%:=student.avatar%>" alt="">
			</span>
		</li>
		<li class="space"></li>
		<li>
			<span class="name">
				姓名
			</span>
			<span class="val">
				<input id="student_name" type="text" placeholder="请输入您的姓名" value="<%:=student.name%>">
			</span>
		</li>
		<li class="in">
			<span class="name">
				性别
			</span>
			<span class="val">
				<select id="student_sex" class="hide formelm">
					<option value="">请选择</option>
					<option value="1">男</option>
					<option value="2">女</option>
				</select>
			</span>
		</li>
		<li>
			<span class="name">
				年龄
			</span>
			<span class="val">
				<input id="student_age" class="student_num" type="number" placeholder="请填入年龄" value=<%:=student.age%>>
			</span>
		</li>
		<li class="space"></li>
		<li class="in">
			<span class="name">
				省份
			</span>
			<span id="sel_student_province" class="val"></span>
		</li>
		<li>
			<span class="name">
				当前所在学校
			</span>
			<span class="val">
				<input id="student_currentschool" type="text" placeholder="填写目前就读的学校"
				       value="<%:=student.currentschool%>">
			</span>
		</li>
		<li class="space"></li>
		<li class="noborder">
			<span class="name">
				艺考方向
			</span>
			<span class="val">
				<span class="intro">
					点击下方标签选择
				</span>
			</span>
		</li>
		<li id="sel_classify" class="checks_cont">
			<% for(var i in classify){if(i>0){
				var thisActive = '';
				for(var index in student.target){
					if( student.target[index]==classify[i].id ){
						thisActive = 'active';
					}
				}
			%>
				<a data_id="<%:=classify[i].id %>" class="<%:=thisActive%>" href="javascript:;">
					<%:=classify[i].name %>
				</a>
			<% }
			} %>
		</li>
		<li class="in">
			<span class="name">
				目标院校
			</span>
			<span class="val">
				<input id="student_targetschool" readonly="readonly" type="text" placeholder="请选择目标学校">
			</span>
		</li>
		<li>
			<span class="name">
				文化分
			</span>
			<span class="val">
				<input id="student_achievement" type="number" class="student_num" placeholder="文化分"
				       value=<%:=student.achievement%>>
			</span>
		</li>

		<%} else { var mechanism = data.mechanism%>

		<li class="app_formlist_cover">
			<span class="name">
				机构封面图
			</span>
			<span class="val">
				<img id="mechanism_cover" src="<%:=mechanism.cover%>" alt="">
			</span>
		</li>
		<li class="space"></li>
		<li class="app_formlist_avatar">
			<span class="name">
				机构头像
			</span>
			<span class="val">
				<img id="mechanism_avatar" src="<%:=mechanism.avatar%>" alt="">
			</span>
		</li>
		<li>
			<span class="name">
				机构名称
			</span>
			<span class="val">
				<input id="mechanism_name" type="text" placeholder="机构全称" value="<%:=mechanism.name%>">
			</span>
		</li>
		<li>
			<span class="name">
				机构电话
			</span>
			<span class="val">
				<input id="mechanism_tel" type="text" placeholder="机构电话，例如:400-888-8888" value="<%:=mechanism.tel%>">
			</span>
		</li>
		<li class="noborder">
			<span class="name">
				教授课程
			</span>
			<span class="val">
				<span class="intro">
					点击下方标签选择
				</span>
			</span>
		</li>
		<li id="sel_classify" class="checks_cont">
			<% for(var i in classify){if(i>0){
					for(var thisIndex in mechanism.curriculum){
						if( mechanism.curriculum[thisIndex]===classify[i].id ){
							var thisActive=true;
						}
					}
			%>
			<a data_id="<%:=classify[i].id %>" class="<%if(thisActive===true){%>active<%}%>" href="javascript:;">
				<%:=classify[i].name %>
			</a>
			<% }} %>
		</li>
		<li class="space"></li>
		<li class="in">
			<span class="name">
				机构地址
			</span>
			<span class="val">
				<ul id="area_list" style="display:none;"></ul>
			</span>
		</li>
		<li class="nopadding pat">
			<span class="val">
				<textarea id="mechanism_address" placeholder="请填写详细地址，不少于5个字。"></textarea>
			</span>
		</li>
		<li class="space"></li>
		<li>
			<span class="name">
				简介
			</span>
		</li>
		<li class="nopadding">
			<span class="val">
				<textarea id="mechanism_intro" placeholder="不超过300字。"></textarea>
			</span>
		</li>
		<li class="space"></li>
		<li>
			<span class="name">
				相册
			</span>
		</li>
		<li class="nopadding" id="mechanism_slide">
			<span class="val qualifications clearfix">
				<img src="../../lib/img/prod_img_cache.gif" class="minimize" alt="">
			</span>
		</li>
		<li class="space"></li>
		<li>
			<span class="name">
				资质
			</span>
		</li>
		<li class="nopadding" id="mechanism_qualifications">
			<span class="val qualifications clearfix">
				<img src="../../lib/img/prod_img_cache.gif" class="minimize" alt="">
			</span>
		</li>
		<%}%>
	</ul>
	<a href="javascript:;" class="skin_btn" tapmode onclick="submitData()">提交</a>
</script>
<script id="sel_student_province_template" type="text/html">
	<select id="student_province">
		<% for(var i in provinceData){ var province = provinceData[i] %>
		<option value="<%:=province.id%>"><%:=province.name%></option>
		<% } %>
	</select>
</script>
<script id="area_template" type="text/html">
	<% for(var i in provinceData){ var province = provinceData[i] %>
	<li>
		<span data-id="<%:=province.id%>"><%:=province.name%></span>
		<ul>
			<% for(var j in province.city){ var city = province.city[j]%>
			<li data-id="<%:=city.id%>">
				<%:=city.name %>
				<ul>
					<% for(var k in city.area){ var area = city.area[k]%>
					<li data-id="<%:=area.id%>"><%:=area.name %></li>
					<% } %>
				</ul>
			</li>
			<% } %>
		</ul>
	</li>
	<% } %>
</script>
<script>
	var
		//准备的提交动作
		submitData = function () {
		},
		setKeyWord = function () {
		},
		setImgs=function(data){
			var _room = $('#mechanism_qualifications').find('.qualifications'),
				str = '';
			for (var i in data) {
				str += '<img src="' + data[i].thumb + '" class="minimize" alt="">'
			}
			_room.html(str);
		},
		setImgs1=function(data){
			var _room = $('#mechanism_slide').find('.qualifications'),
				str = '';
			for (var i in data) {
				str += '<img src="' + data[i].thumb + '" class="minimize" alt="">'
			}
			_room.html(str);
		},
		//验证后的提交动作
		ajaxData = function (data) {
			_$.loading.start();
			_$.ajax({
				interface: 'userIntroUpdate',
				data: data,
				call: function (data) {
					if (data.status === 200) {
						var _data_ = data.data;
						_$.db.ext({
							val: _data_,
							call: function (data) {
								_$.db.ext({
									key: 'appStatus',
									val: {
										loginStatusChange: true
									},
									call: function () {
										api.closeWin();
									}
								});
								_$.loading.end();
							}
						})
					} else {
						_$.loading.end();
						api.toast({
							msg: data.msg
						})
					}
				}
			})
		},
		appready = function () {

			//确定是不是注册学生，bool值。
			var isStudent = api.winName === 'student',
				//确定提交的数据格式
				pageData = {
					regType: api.winName,
					thisName: isStudent ? '个人' : '机构',
					student: {
						avatar: '../../lib/img/default_student_avatar.png',
						name: undefined,
						sex: undefined,
						age: undefined,
						area: undefined,
						currentschool: undefined,
						target: undefined,
						targetschool: undefined,
						achievement: undefined
					},
					mechanism: {
						cover: '../../lib/img/prod_img_cache.gif',
						avatar: '../../lib/img/default_mechanism_avatar.png',
						name: undefined,
						tel: undefined,
						curriculum: undefined,
						area: {
							province: null,
							city: null,
							area: null
						},
						address: '',
						introduce: '',
						qualifications: undefined
					}
				},
				_classify_ = undefined;

			_$.db.get({
				call: function (data) {
					if (data.type === 1) {
						pageData.student = {
							avatar: data.avatar,
							name: data.name,
							sex: data.sex,
							age: data.age,
							area: data.s_province,
							currentschool: data.school,
							target: data.curriculum,
							targetschool: data.m_school,
							achievement: data.integral
						}
					}
					if (data.type === 2) {
						pageData.mechanism = {
							cover: data.cover,
							avatar: data.avatar,
							name: data.name,
							slide: data.slide,
							tel: data.tel,
							curriculum: data.curriculum,
							area: {
								province: data.s_province,
								city: data.s_city,
								area: data.s_county
							},
							address: data.address,
							introduce: data.introduce,
							qualifications: data.qualifications
						}
					}
				}
			});

			_$.db.get({
				key: 'classify',
				call: function (data) {
					_classify_ = data;
				}
			});

			_$.template('usersel_template', {data: pageData, classify: _classify_}, function (html) {

				$('#usersel').html(html);
				$('.nocontent').hide();

				//获取选择的目标院校的方法
				setKeyWord = function (key, id) {
					pageData.student.targetschool = id;
					$('#student_targetschool').val(key);
				}

				//机构选择专业和学生选择艺考专业的逻辑
				$('#sel_classify a').on('click', function () {
					$(this).toggleClass('active');
					var _allElm = $('#sel_classify a.active'),
						_arr = [];
					_allElm.map(function (i, j) {
						_arr.push(parseInt($(j).attr('data_id')))
					});
					pageData.student.target = pageData.mechanism.curriculum = _arr;
				})

				//机构封面和机构资质的点击逻辑
				$('#mechanism_cover').on('click', function () {
					_$.img.getImg({
						max: 1,
						rfrom: 'mposter',
						call: function (data) {
							$('#mechanism_cover').attr('src', data[0].thumb);
						}
					})
				})

				$('#mechanism_slide').on('click', function () {
					_$.img.galleryImg({
						max: 4,
						rfrom: 'slide',
						call: function (data) {
							setImgs1(data);
						}
					})
				})

				$('#mechanism_qualifications').on('click', function () {
					_$.img.galleryImg({
						max: 4,
						rfrom: 'client',
						call: function (data) {
							setImgs(data);
						}
					})
				})

				$('#mechanism_avatar, #student_avatar').on('click', function () {
					var _id_ = $(this).attr('id');
					_$.img.getImg({
						max: 1,
						rfrom: _id_ === 'mechanism_avatar' ? 'mavatar' : 'uavatar',
						call: function (data) {
							$('#' + _id_).attr('src', data[0].thumb);
						}
					})
				})

				if (isStudent) {

					$('#student_sex').mobiscroll().select({
						mode: 'scroller',
						display: 'bottom',
						lang: 'zh',
						placeholder: '请选择性别',
						onSelect: function (valueText, inst) {
							var val = parseInt($('#student_sex').val());
							pageData.student.sex = val === 0 ? undefined : val;
						}
					});
					$("#student_targetschool").on('click', function () {
						api.openWin({
							name: 'targetschool',
							url: 'targetschool.html',
							pageParam: {
								win: api.winName
							}
						})
					})

					//是学生，覆盖提交动作的内容
					submitData = function () {
						//声明变量，用来存储提交数据的那些input的值
						var _student_ = pageData.student;
						_student_.avatar = $('#student_avatar').attr('src');
						_student_.name = $('#student_name').val();
						_student_.age = parseInt($('#student_age').val());
						_student_.currentschool = $('#student_currentschool').val();
						_student_.achievement = parseInt($('#student_achievement').val());

						if (_student_.avatar === '../../lib/img/default_student_avatar.png') {
							_student_.avatar = null;
						}
						if (!_$.dataTest('notempty', _student_.name)) {
							api.toast({
								msg: '没有填写姓名'
							})
							return;
						}
						if (!_$.dataTest('num', _student_.sex)) {
							api.toast({
								msg: '没有选择性别'
							})
							return;
						}
						if (!_$.dataTest('num', _student_.age)) {
							api.toast({
								msg: '没有填写年龄'
							})
							return;
						}
						if (!_$.dataTest('num', _student_.area)) {
							api.toast({
								msg: '没有选择省份'
							})
							return;
						}
						if (!_$.dataTest('notempty', _student_.currentschool)) {
							api.toast({
								msg: '没有填写目前就读的学校'
							})
							return;
						}
						if (!_$.dataTest('notempty', _student_.currentschool)) {
							api.toast({
								msg: '没有填写目前就读的学校'
							})
							return;
						}
						if (_$.getType(_student_.target) !== 'array' || _student_.target.length === 0) {
							api.toast({
								msg: '需要选择至少一项艺考方向'
							})
							return;
						}
						if (_student_.targetschool === undefined) {
							api.toast({
								msg: '没有选择目标院校'
							})
							return;
						}
						if (!_$.dataTest('num', _student_.achievement)) {
							api.toast({
								msg: '没有填写文化分'
							})
							return;
						}

						ajaxData(pageData);

					}

				} else {
					//机构选择地区的逻辑
					_$.template('area_template', __areaData__, function (html) {
						$('#area_list').html(html).mobiscroll().treelist({
							lang: 'zh',
							display: 'bottom',
							fixedWidth: [100, 100, 100],
							inputClass: 'area_input',
							placeholder: '请选择机构地址',
							labels: ['省', '市', '区'],
							headerText: function (valueText) {
								return "请选择机构地址"
							},
							rows: 5,//滚动区域内的行数
							showLabel: false,
							formatResult: function (array) { //返回自定义格式结果
								var province = __areaData__.provinceData[array[0]],
									city = province.city[array[1]],
									area = city.area[array[2]];
								if (province.id === 0) {
									return;
								} else {
									pageData.mechanism.area = {
										province: province.id,
										city: city.id,
										area: area.id
									}
									return province.name + ' ' + city.name + ' ' + area.name;
								}
							}
						});
					})

					//覆盖提交动作的内容
					submitData = function () {

						//声明变量，用来存储提交数据的那些input的值
						var _mechanism_ = pageData.mechanism;

						_mechanism_.cover = $('#mechanism_cover').attr('src');
						_mechanism_.avatar = $('#mechanism_avatar').attr('src');
						_mechanism_.name = $('#mechanism_name').val();
						_mechanism_.tel = $('#mechanism_tel').val();
						_mechanism_.address = $('#mechanism_address').val();
						_mechanism_.introduce = $('#mechanism_intro').val();
						_mechanism_.qualifications = [];
						_mechanism_.slide = [];

						if (_mechanism_.avatar === '../../lib/img/default_mechanism_avatar.png') {
							_mechanism_.avatar = null;
						}
						$('#mechanism_qualifications img').map(function (i, j) {
							var src = $(j).attr('src');
							if (src === '../../lib/img/prod_img_cache.gif') {
								return;
							} else {
								_mechanism_.qualifications.push(src)
							}
						})
						$('#mechanism_slide img').map(function (i, j) {
							var src = $(j).attr('src');
							if (src === '../../lib/img/prod_img_cache.gif') {
								return;
							} else {
								_mechanism_.slide.push(src)
							}
						})
						if (!_$.dataTest('notempty', _mechanism_.name)) {
							api.toast({
								msg: '没有填写机构名'
							})
							return;
						}
						if (!_$.dataTest('notempty', _mechanism_.tel)) {
							api.toast({
								msg: '没有填写机构电话'
							})
							return;
						}
						if (_$.getType(_mechanism_.curriculum) !== 'array' || _mechanism_.curriculum.length === 0) {
							api.toast({
								msg: '需要选择至少一项教授课程'
							})
							return;
						}
						if (_mechanism_.area.province === null) {
							api.toast({
								msg: '没有选择省/市/区'
							})
							return;
						}
						if (_mechanism_.address.length < 5) {
							api.toast({
								msg: '重新填写详细机构地址'
							})
							return;
						}
						if (_mechanism_.introduce.lenght==0) {
							api.toast({
								msg: '没有填写机构简介'
							})
							return;
						} else if (_mechanism_.introduce.lenght > 300) {
							api.toast({
								msg: '请控制简介在300字以内'
							})
							return;
						}
						if (_mechanism_.qualifications.length === 0) {
							api.toast({
								msg: '没有上传资质证明'
							})
							return;
						}

						ajaxData(pageData);

					}
				}
				_$.template('sel_student_province_template', __areaData__, function (html) {
					$('#sel_student_province').html(html);
					$('#student_province').mobiscroll().select({
						mode: 'scroller',
						display: 'bottom',
						lang: 'zh',
						placeholder: '请选择省份',
						onSelect: function (valueText, inst) {
							pageData.student.area = undefined;
							for (var i in __areaData__.provinceData) {
								if (!pageData.student.area && __areaData__.provinceData[i].name == valueText) {
									pageData.student.area = __areaData__.provinceData[i].id;
								}
							}
						}
					});
				});

				_$.db.get({
					call: function (data) {
						if (data.type === 1) {
							//默认值的付给动作
							$('#student_sex_dummy').val(pageData.student.sex===1?'男':'女');
							$('#student_targetschool').val(__returnSchoolData__(pageData.student.targetschool).name);
							$('#student_province_dummy').val(__returnAreaData__(pageData.student.area).name);
						}
						if (data.type === 2) {
							var str =''
							str += __returnAreaData__(data.s_province).name+' ';
							str += __returnAreaData__(data.s_city).name+' ';
							str += __returnAreaData__(data.s_county).name;
							$('#area_list_dummy').val(str);
							$('#mechanism_intro').val(data.introduce);
							$('#mechanism_address').val(data.address);
							$('#mechanism_introduce').val(data.introduce);

							var imgs = []
							for(var i in data.qualifications){
								imgs.push({
									thumb:data.qualifications[i]
								})
							}
							setImgs(imgs);
							imgs = [];
							for(var i in data.slide){
								imgs.push({
									thumb:data.slide[i]
								})
							}
							setImgs1(imgs);

						}
					}
				});

				_$.sleep(function () {
					$('.app_head .name').html(pageData.thisName + '资料完善');
					$('#usersel').show();
				}, 250);

			})
		};
</script>
<script src="../../lib/js/config.js"></script>
<script src="../../lib/js/areaData.js"></script>
<script src="../../lib/js/schoolData.js"></script>
<script src="../../lib/js/jquery.js"></script>
<script src="../../lib/js/jquery.mobiscroll.js"></script>
<script src="../../lib/js/common.js"></script>
</body>
</html>